package by.bsu.rfe.belianko.euniversity.controller.command;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import by.bsu.rfe.belianko.euniversity.constants.ErrorMessages;
import by.bsu.rfe.belianko.euniversity.constants.LoggerMessages;
import by.bsu.rfe.belianko.euniversity.constants.PageNames;
import by.bsu.rfe.belianko.euniversity.dao.IFacultiesDAO;
import by.bsu.rfe.belianko.euniversity.dao.IStudentsDAO;
import by.bsu.rfe.belianko.euniversity.exceptions.CommandException;
import by.bsu.rfe.belianko.euniversity.exceptions.DaoTechnicalException;
import by.bsu.rfe.belianko.euniversity.model.DataBaseLayer;
import by.bsu.rfe.belianko.euniversity.model.Faculty;
import by.bsu.rfe.belianko.euniversity.model.Student;

/**
 * Enrolls student on faculty.
 * 
 * @author Vitali_Belianko
 * 
 */
public class EnrollCommand implements Command {

	private static final String ENROLLED_ON = " enrolled on ";
	private static final String STUDENT2 = "Student: ";
	private static final String FACULTY_ID = "facultyId";
	private static final String STUDENT = "student";
	private static final String FACULTY = "faculty";
	private static final String CERTIFICATE = "certificate";
	private static final String MARK3 = "mark3";
	private static final String MARK2 = "mark2";
	private static final String MARK1 = "mark1";
	private static final IStudentsDAO studentsDao = DataBaseLayer.getStudentsDao();
	private static final IFacultiesDAO facultiesDao = DataBaseLayer.getFacultiesDAO();
	private static final Logger LOGGER = Logger.getLogger(EnrollCommand.class);

	/**
	 * Enrolls student to the selected faculty.
	 * 
	 * @param request
	 * 
	 * @return cabinet page
	 * @throws CommandException
	 */
	@Override
	public String execute(HttpServletRequest request) throws CommandException {

		LOGGER.debug(LoggerMessages.STARTED_COMMAND);

		Faculty faculty = null;
		Student student;

		int mark1 = Integer.parseInt(request.getParameter(MARK1));
		int mark2 = Integer.parseInt(request.getParameter(MARK2));
		int mark3 = Integer.parseInt(request.getParameter(MARK3));
		int certificate = Integer.parseInt(request.getParameter(CERTIFICATE));
		int totalScore = mark1 + mark2 + mark3 + certificate;

		student = (Student) request.getSession(true).getAttribute(STUDENT);
		long facultyId = Long.parseLong(request.getParameter(FACULTY_ID));
		long id = student.getId();

		try {
			studentsDao.enrollInFaculty(facultyId, id, totalScore);
		} catch (DaoTechnicalException e) {
			LOGGER.error(e.getMessage());
			throw new CommandException(ErrorMessages.CAN_NOT_ENROLL_ON_FACULTY, e);
		}
		try {
			faculty = facultiesDao.getFacultyById(facultyId);
		} catch (DaoTechnicalException e) {
			LOGGER.error(e.getMessage());
			throw new CommandException(ErrorMessages.CAN_NOT_GET_FACULTY, e);
		}

		LOGGER.debug(STUDENT2 + student.getName() + ENROLLED_ON + faculty.getFacultyName());

		student.setFaculty(faculty);
		student.setTotalScore(totalScore);
		request.getSession(true).setAttribute(STUDENT, student);
		request.getSession(true).setAttribute(FACULTY, faculty);

		LOGGER.debug(LoggerMessages.FINISHED_COMMAND);

		return PageNames.CABINET_PAGE;
	}

}
